<template>
	<view>
	<view class="cart-container" v-if="cart.length !== 0">
		<!-- 收货地址组件 -->
		<my-address></my-address>
		<!-- 商品列表的标题区域 -->
		<view class="cart-title">
			<uni-icons type="shop" size="17"></uni-icons>
			<text class="cart-title-text">购物车</text>
		</view>
		<!-- 商品列表区域 -->
		<uni-swipe-action>
			<block v-for="(good,i) in cart" :key="i">
				<uni-swipe-action-item :options="options" @click="swipeItemClickHandler(good)">
					<my-goods :item="good">
						<radio :checked="good.goods_state" color="#C00000" @click="radioOnclickHandler(good)" slot="radio"></radio>
						<uni-number-box class="uni-number-box" slot="uni-number-box" :min="1" :value="good.goods_count" @change="numberChange($event,good)"></uni-number-box>
					</my-goods>
				</uni-swipe-action-item>
			</block>
		</uni-swipe-action>
		<!-- 结算区域 -->
		<my-settle></my-settle>
	</view>
	<!-- 空空如也 -->
		<view class="empty-cart" :style="{height: wh + 'px'}" v-else>
			<image class="empty-image" src="../../static/empty-cart.png" mode="widthFix"></image>
			<text class="empty-text">空空如也~</text>
			<button  class="to-shpoList" @click="goToCate">去逛逛</button>
		</view>

	</view>
</template>

<script>
	import badgeMix from '@/maxins/Badge-tabbar.js'
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		mixins: [badgeMix],
		data() {
			return {
				options:[
					{
						text:"删除",
						style:{
							backgroundColor:'#C00000'
							},
					}
				],
				wh:0,
				good: {},
				// 勾选框点击后参数存到这里
				goodsClickInfo: {
					goods_id: 0,
					goods_state: true
				},
				// 数量加减框点击后参数存到这里
				numBoxInfo: {
					goods_id2: 0,
					goods_count: 0
				},
				deleteGoodsInfo:{
					goods_id3:0
				},
				vModelValue: 0
			};
		},
		onLoad() {
			const sysInfo = uni.getSystemInfoSync()	
			this.wh = sysInfo.windowHeight
		},
		
		computed: {
			...mapState('my_cart', ['cart'])
		},
		methods: {
			...mapMutations('my_cart', ['updateGoodsState', 'updateGoodsCount','deleteGoodsItem']),
			// 两个参数当前点击的商品信息good,和它的索引i
			// 当点击勾选框时收集商品的id和state存到data里并且点击后吧状态取反赋给cart里的goods_state
			// 这里肯能稍微难以理解，但仔细一想就是这么一回事（自己写的，肯定能猜出来）
			radioOnclickHandler(good) {
				this.goodsClickInfo.goods_id = good.goods_id,
					this.goodsClickInfo.goods_state = !good.goods_state
				this.updateGoodsState(this.goodsClickInfo)

			},
			numberChange(value, good) {
				this.numBoxInfo.goods_id2 = good.goods_id
				this.numBoxInfo.goods_count = value * 1
				this.updateGoodsCount(this.numBoxInfo)
				
			},
			swipeItemClickHandler(good){
				this.deleteGoodsInfo.goods_id3 = good.goods_id
				this.deleteGoodsItem(this.deleteGoodsInfo)
			},
			goToCate(){
				uni.switchTab({
					url:'../cate/cate'
				})
			}

		}


	}
</script>

<style lang="scss">
	.cart-title {
		height: 40px;
		display: flex;
		align-items: center;
		border-bottom: 1px solid #EFEFEF;
		padding-left: 5px;

		.cart-title-text {
			font-size: 14px;
			margin-left: 10px;
		}
	}
	.cart-container{
		padding-bottom: 50px;
	}
	.empty-cart{
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		background-color: rgb(246,246,246);
		.empty-image{
			width: 200px;
			height: 200px;
			border-radius: 55px;
		}
		.empty-text{
			color: #847b77;
			font-size: 14px;
		}
		.to-shpoList{
			padding: 0;
			width: 90px;
			height: 35px;
			border-radius: 50px;
			border: 1px solid rgb(254,115,0) !important;
			background-color: rgb(246,246,246);
			font-size: 12px;
			color: rgb(254,115,0);
			line-height: 35px;
			margin-top: 20px;
			list-style: none;
			transform: background-color 1s linear;
		}
	}
</style>
